/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package damagecalculator;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import javax.swing.JButton;

/**
 *
 * @author Marcelo
 */
public class ItemsButton extends JButton implements ActionListener {

    public ItemsButton(String name) {
        super(name);
        init();
    }

    private void init() {
        addActionListener(this);
    }

    
    public void actionPerformed(ActionEvent e) {        
        int x = 0;
        while (x <= 5 && DamageCalculatorView.BuildButtons[x] != null) { // Look for a free spot in BuildButtons[], if found break.
            if (DamageCalculatorView.BuildButtons[x] == null) {
                break;
            }
            x++;
        }
        if (x <= 5) { // If x > 5, might cause ArrayOutOfBound Exception in ItemBuildPanels[].
            DamageCalculatorView.BuildButtons[x] = new RemovableButton(this.getText()); // Create and add RemovableButton to BuildButtons[], with the text of the button pressed.
            DamageCalculatorView.BuildButtons[x].setBounds(0, 0, DamageCalculatorView.ItemBuildPanels[x].getWidth(),
                    DamageCalculatorView.ItemBuildPanels[x].getHeight()); // Set the size (bounds) of the button to be as big as the container (JPanel) around it.
            DamageCalculatorView.BuildButtons[x].setContainer(DamageCalculatorView.ItemBuildPanels[x]); // Set the variable of the container, used in the RemovableButton class.
            DamageCalculatorView.BuildButtons[x].setArrayPos(x); // Set the place the button holds in the BuildButtons[] array, used in the RemovableButton class.
            DamageCalculatorView.BuildButtons[x].setToolTipText(this.getToolTipText());
            DamageCalculatorView.ItemBuildPanels[x].add(DamageCalculatorView.BuildButtons[x]); // Add the button to the container.
            DamageCalculatorView.ItemBuildPanels[x].validate(); // Validate the changes to the container.
            DamageCalculatorView.ItemBuildPanels[x].repaint(); // Refresh the container.
        }

    }
}